package com.neuedu.my12306.web;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.neuedu.my12306.entity.UserInfo;
import com.neuedu.my12306.service.IUserInfoService;
import com.neuedu.my12306.util.SpringContextUtil;
import com.neuedu.my12306.util.StringUtil;

/**
 * 用户列表分页查询
 */
@WebServlet("/UserAdminServlet.do")
public class UserAdminServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    private IUserInfoService userInfoService = null;   
    public UserAdminServlet() {
        super();
        //使用IUserInfoService接口解耦合 CheckUserNameServlet  与IUserInfoServiceImpl 的耦合关系
        // userInfoService = new IUserInfoServiceImpl();
         userInfoService = SpringContextUtil.getBean("userService", IUserInfoService.class);
        
    }
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//1.在接收数据前进行编码转换(统一使用过滤器转换)
		//2.接受用户输入的参数
		String pageString = request.getParameter("page");
		String sizeString = request.getParameter("size");
		int page = StringUtil.parseInt(pageString, 1);
		int size = StringUtil.parseInt(sizeString, 3);
		//3.进行分页查询
		List<UserInfo> list = userInfoService.findUserInfos(page, size);
		
		//查询出总行数
		int total = userInfoService.findTotal();
		//根据总行数和分页参数计算分页
		int pages = total%size == 0 ? total/size  : total/size+1;
		int prePage = page-1==0?1:page-1;//上一页
		int nextPage = page+1 ==pages? pages:page+1;//下一页
		
		
		
		//4.跳转到用户列表页面中，并把分页查询到的数据传递到列表页进行显示
		request.setAttribute("userInfoList", list);
		request.setAttribute("pages", pages);
		request.setAttribute("prePage", prePage);
		request.setAttribute("nextPage", nextPage);
		request.setAttribute("size", size);
		request.getRequestDispatcher("Admin/UserManageQuery.jsp").forward(request, response);
		
	}

}
